import qs from 'qs'
const common = {
  isNull: (dataSource, that, title, type, userType, pieTitle, fieldType) => {
    let yaisx
    let days = []
    if (that.areaName === '全国战区') {
      let data = dataSource.resultIndexGroupByTime
      for (let i in data) {
        days.push(data[i].timeId)
      }
      yaisx = common.maxValue(data, that, 'count', type, userType)
    } else {
      for (let i in dataSource.resultIndexGroupByTime) {
        days.push(dataSource.resultIndexGroupByTime[i].timeId)
      }
      yaisx = common.maxValue(dataSource.resultIndexGroupByTime, that, 'count', type, userType)
    }
    that.$refs.lineDom.xAxisDatas(days, title, yaisx.yaisData, null, yaisx.unit)
    that.$refs.vBarDom.xAxisDatas(days, that.vBarTitle, yaisx.yaisData, null, yaisx.unit)
  },
  // 首页发送请求封装
  homeRequest (type, startDate, endDate, that, adress) {
    // type 1、当天   2、前一周   3、前一月   4、前一季度 adress请求地址
    let token = window.sessionStorage.getItem('token')
    that.isShowMask = true
    that.$api.post('/index/' + adress, qs.stringify({
      token: token,
      type: type,
      startDate: startDate,
      endDate: endDate
    })).then(function (res) {
      if (res.status === 200) {
        that.isShowMask = false
        let data = JSON.stringify(res.data)
        sessionStorage.setItem('getHomeData', data)
        common.requestIsUserType(that, type)
      }
    })
  },
  dataQueryCommon: (index, that, dataSource, title, type, userType, pieTitle, fieldType, adress) => {
    // 首页才使用该函数
    let days = []
    let yaisx
    if (index === 0) {
      common.homeRequest(1, null, null, that, adress)
    } else if (index === 1) {
      common.homeRequest(2, null, null, that, adress)
    } else if (index === 2) {
      common.homeRequest(3, null, null, that, adress)
    } else if (index === 3) {
      common.homeRequest(4, null, null, that, adress)
    }
    yaisx = common.maxValue(dataSource, that, fieldType, type, userType)
    if (that.areaName === '全国战区') {
      const data = dataSource.resultIndexGroupByTime
      for (let k in data) {
        days.push(data[k].TIME_ID)
      }
      yaisx = common.maxValue(data, that, 'count', type, userType)
    } else {
      for (let k in dataSource.resultIndexGroupByTime) {
        days.push(dataSource.resultIndexGroupByTime[k].TIME_ID)
      }
    }
    that.$refs.lineDom.xAxisDatas(days, title, yaisx.yaisData, null, yaisx.unit)
    that.$refs.vBarDom.xAxisDatas(days, that.vBarTitle, yaisx.yaisData, null, yaisx.unit)
  },
  barxAxis: function (dataSource, that, nameType, fieldType, type) {
    // nameType城市或者省市名的字段 fieldType是数量的字段如count
    let xAxisData = []
    let yaisxData = []
    if (!dataSource) {
      return {
        yaisxData,
        xAxisData
      }
    }
    for (let i in dataSource) {
      yaisxData.push(dataSource[i][nameType])
      xAxisData.push(dataSource[i][fieldType])
    }
    let backValue = common.maxValue(dataSource, that, fieldType, type)
    let unit = backValue.unit
    xAxisData = backValue.yaisData
    // yaisxData y轴数据  xAxisData x轴数据

    return {
      yaisxData,
      xAxisData,
      unit
    }
  },
  hotPicPieData: (data, that, name, value) => {
    // 1 已支付 2乘客无责 3 司机无责 4乘客有责 5 司机有责 6未支付
    // userType为1代表全国
    let showDatas = []
    for (let i in data) {
      showDatas.push({
        name: data[i][name],
        value: data[i][value]
      })
    }
    return showDatas
  },
  hotPicLineXdata: (dataSource, type, fieldType) => {
    let days = []
    // type  Integer    1、当天   2、前一周   3、前一月   4、前一季度
    if (!dataSource) {
      return {
        days
      }
    }
    for (let i in dataSource) {
      days.push(dataSource[i][fieldType])
    }
    return {
      days
    }
  },
  maxValue: (dataSource, that, fieldType, type) => {
    // fieldType是传入的字段 type是传入单位为元还是个或者其他
    let unit = ''
    let maxValue
    let yxaisVal = []
    let yaisData = []
    if (!dataSource) {
      return {
        yaisData,
        unit
      }
    }
    for (let k in dataSource) {
      yxaisVal.push((dataSource[k][fieldType]))
    }
    maxValue = Math.max.apply(null, yxaisVal)
    let finnalData
    if (maxValue <= 800) {
      finnalData = 1
      unit = type
    } else if (maxValue >= 800 && maxValue <= 10000) {
      unit = '千' + type
      finnalData = 1000
    } else if (maxValue >= 10000) {
      unit = '万' + type
      finnalData = 10000
    }
    for (let k = 0; k < yxaisVal.length; k++) {
      if (finnalData === 1) {
        yaisData.push((yxaisVal[k] / finnalData).toFixed(2))
      } else {
        yaisData.push((yxaisVal[k] / finnalData).toFixed(2))
      }
    }
    return {
      yaisData,
      unit
    }
  },
  // 首页判断是不是全国
  isUserType: (that, unit) => {
    // unit单位
    if (that.areaName === '全国战区') {
      that.formatter = '{b}\n{c}' + unit + '\n{d}%'
      that.isShowOther = false
    } else {
      that.formatter = '{b}\n{c}' + unit + '\n{d}%'
    }
  },
  // 热力图判断是不是全国
  hotPicIsUserType (that, unit) {
    if (that.areaName === '全国战区') {
      that.isShowOrder = true
      that.isShowBar = false
      that.formatter = '{b}\n{c}' + unit + '\n{d}%'
    } else {
      that.formatter = '{b}\n{c}' + unit + '\n{d}%'
      that.isShowOrder = true
      that.isShowBar = true
    }
  },
  // 请求后判断是不是全国
  requestIsUserType (that, type) {
    if (that.areaName === '全国战区') {
      that.firstTimeInit(1, that, type)
    } else {
      that.firstTimeInit(2, that, type)
    }
  },
  // 点击近昨日到近90天封装
  clickDate: (that, index, adress) => {
    // index 点击传过来的序列号
    if (index === 0) {
      common.homeRequest(1, null, null, that, adress)
    } else if (index === 1) {
      common.homeRequest(2, null, null, that, adress)
    } else if (index === 2) {
      common.homeRequest(3, null, null, that, adress)
    } else if (index === 3) {
      common.homeRequest(4, null, null, that, adress)
    }
  },
  // 首页如果七天没有数据依次显示后面的数据
  homeShowDate (dataSourceTime, userType, that, type) {
    // userTtype 1代表全国 type1代表昨日 2代表7天 3代表30天 4代表90天
    if (type === 1) {
      that.commonInit(dataSourceTime, '昨日', userType, that, 0)
      that.$refs.queryTime.changeNumVal(0)
    } else if (type === 2) {
      that.commonInit(dataSourceTime, '近7天', userType, that, 1)
      that.$refs.queryTime.changeNumVal(1)
    } else if (type === 3) {
      that.commonInit(dataSourceTime, '近30天', userType, that, 2)
      that.$refs.queryTime.changeNumVal(2)
    } else if (type === 4) {
      that.commonInit(dataSourceTime, '近90天', userType, that, 3)
      that.$refs.queryTime.changeNumVal(3)
    }
  },
  // 把数字按进照每三个打一个逗号，起分隔作用
  formatNum (num) {
    let newStr = ''
    let count = 0
    let str = String(num)
    if (str.indexOf('.') === -1) {
      for (let i = str.length - 1; i >= 0; i--) {
        if (count % 3 === 0 && count !== 0) {
          newStr = str.charAt(i) + ',' + newStr
        } else {
          newStr = str.charAt(i) + newStr
        }
        count++
      }
      str = newStr // 自动补小数点后两位
    } else {
      for (let i = str.indexOf('.') - 1; i >= 0; i--) {
        if (count % 3 === 0 && count !== 0) {
          newStr = str.charAt(i) + ',' + newStr
        } else {
          newStr = str.charAt(i) + newStr // 逐个字符相接起来
        }
        count++
      }
      str = newStr + (str + '00').substr((str + '00').indexOf('.'), 3)
    }
    return str
  },
  // 判断是否是线路是线路就加上往返二字
  isLine () {
    let getRole = JSON.parse(sessionStorage.getItem('getRole'))
    let userType = getRole.userType
    let isLine = ''
    if (userType === 6) {
      isLine = '（往返）'
    }
    return isLine
  },
  detailTitle: [{
    '订单号': 'orderNo',
    '订单金额': 'amount',
    '乘客电话': 'memberPhone',
    '订单状态': 'orderState',
    '付款方式': 'payState',
    '起始点': 'startLocation',
    '终点': 'endLocation',
    '乘客姓名': 'memberName',
    '坐车次数': 'memberCount',
    '乘客生日': 'memberBirthday',
    '乘客职业': 'memberJob',
    '注册时间': 'createDate'
  }, {
    '车牌号': 'carNo',
    '车辆类型': 'carType',
    '所有人': 'owner',
    '注册地': 'regCity',
    '车辆状态': 'carState',
    '注册日期': 'regDate',
    '核定载人数': 'passengerNum',
    'VIN码': 'vin',
    '车辆颜色': 'carColor',
    '最近一次维修': 'maintainDate',
    '身份证号码': 'identifierNo'
  }, {
    '车牌号': 'carNo',
    '所有人': 'owner',
    '服务分': 'serviceScore',
    '昨日在线时长': 'onLine',
    '车辆类型': 'carType',
    '注册地': 'regCity',
    '车辆状态': 'carState',
    '注册日期': 'regDate',
    '核定载人数': 'passengerNum',
    'VIN码': 'vin',
    '车辆颜色': 'carColor',
    '最近一次维修': 'maintainDate',
    '身份证号码': 'identifierNo'
  }, {
    '线路名称': 'lineName',
    '始发地': 'startLocation',
    '终点': 'endLocation',
    '实际里程数（公里）': 'distance',
    '时长（分钟）': 'spentTime',
    '路桥费': 'tollFee',
    '创建时间': 'createDate',
    '最低配单人数': 'orderPeople'
  }, {
    '运营商名称': 'name',
    '负责人': 'owner',
    '负责人电话': 'phone',
    '部门分成比例': 'departmentRatio',
    '司机分成比例': 'driverRatio',
    '纳税人识别号': 'licencenNo',
    '开户行': 'bankName',
    '银行卡号': 'bankNo',
    '创建日期': 'createDate'
  }, {
    '订单号': 'orderNo',
    '订单金额': 'amount',
    '乘客电话': 'memberPhone',
    '订单状态': 'orderState',
    '付款方式': 'payState',
    '起始点': 'startLocation',
    '终点': 'endLocation',
    '司机姓名': 'driverName',
    '司机手机': 'driverPhone',
    '创建时间': 'createDate'
  }, {
    '订单号': 'orderNo',
    '现金券金额': 'amount',
    '乘客电话': 'memberPhone',
    '订单状态': 'orderState',
    '付款方式': 'payState',
    '起始点': 'startLocation',
    '终点': 'endLocation',
    '乘客姓名': 'memberName',
    '坐车次数': 'memberCount',
    '乘客生日': 'memberBirthday',
    '乘客职业': 'memberJob',
    '使用时间': 'createDate'
  }],
  toDetailRequest (that, item, adress, num) {
    let token = window.sessionStorage.getItem('token')
    that.$api.post('/detail/' + adress + '', qs.stringify({
      token: token,
      id: item.id
    })).then(res => {
      that.leftVal = '0%'
      console.log(res.data.resultData)
      let resultData = res.data.resultData
      let detailTitle = common.detailTitle[num]
      let detailData = []
      for (let k in detailTitle) {
        detailData.push({
          name: k,
          value: resultData[detailTitle[k]]
        })
      }
      that.detailData = detailData
    }).catch(err => {
      console.log(err)
    })
  }
}

export default common
